package ssi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import ssi.tools.Connector;


/**
 * Servlet implementation class InformationUserServlet
 */
@WebServlet("/InformationUserServlet")
public class InformationUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		if (request.getParameter("userId") != null) {
			response.setContentType("application/json");      
			PrintWriter out = response.getWriter();
			int user = Integer.parseInt(request.getParameter("userId"));
			try {
				Connection connection = new Connector().getConnection();
				Statement statement = connection.createStatement();
				ResultSet result = statement.executeQuery("SELECT email, password, type, name, surname, address,pesel FROM `users` WHERE id="+ user);
				if (result.next()) {
					JSONObject jsonReturn = new JSONObject();
					jsonReturn.put("email", result.getString(1));
					jsonReturn.put("password", result.getString(2));
					jsonReturn.put("type", result.getString(3));
					jsonReturn.put("name", result.getString(4));
					jsonReturn.put("surname", result.getString(5));
					jsonReturn.put("address", result.getString(6));
					jsonReturn.put("pesel", result.getString(7));
					out.print(jsonReturn);
					out.flush();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}
}



































